import {
  Controller,
  Get,
  Post,
  Body,
  Request,
  UseGuards,
  BadRequestException,
  Query,
} from '@nestjs/common';
import { AuthGuard } from '@nestjs/passport';
import { VipService } from './vip.service';

@Controller('vip')
export class VipController {
  constructor(private readonly vipService: VipService) {}

  // 获取所有VIP套餐
  @Get('packages')
  async getAllVipPackages() {
    try {
      const result = await this.vipService.getAllVipPackages();
      return {
        success: true,
        data: result,
        message: '获取VIP套餐成功',
      };
    } catch (error) {
      return {
        success: false,
        data: null,
        message: '获取VIP套餐失败: ' + error.message,
      };
    }
  }

  // 获取用户VIP状态
  @UseGuards(AuthGuard('jwt'))
  @Get('status')
  async getUserVipStatus(@Request() req) {
    try {
      const userId = parseInt(req.user.id);
      const result = await this.vipService.getUserVipStatus(userId);
      return {
        success: true,
        data: result,
        message: '获取用户VIP状态成功',
      };
    } catch (error) {
      return {
        success: false,
        data: null,
        message: '获取用户VIP状态失败: ' + error.message,
      };
    }
  }

  // 使用粉钻购买VIP
  @UseGuards(AuthGuard('jwt'))
  @Post('purchase')
  async purchaseVipWithDiamonds(
    @Request() req,
    @Body() body: { packageId: number },
  ) {
    try {
      const userId = parseInt(req.user.id);
      const { packageId } = body;

      if (!packageId) {
        throw new BadRequestException('缺少套餐ID');
      }

      const result = await this.vipService.purchaseVipWithDiamonds(
        userId,
        packageId,
      );
      return result;
    } catch (error) {
      return {
        success: false,
        data: null,
        message: '购买VIP失败: ' + error.message,
      };
    }
  }

  // 充值粉钻（模拟接口，实际应通过支付回调处理）
  @UseGuards(AuthGuard('jwt'))
  @Post('recharge')
  async rechargeDiamonds(
    @Request() req,
    @Body() body: { amount: number; diamonds: number },
  ) {
    try {
      const userId = parseInt(req.user.id);
      const { amount, diamonds } = body;

      if (!amount || !diamonds) {
        throw new BadRequestException('缺少参数');
      }

      const result = await this.vipService.rechargeDiamonds(
        userId,
        amount,
        diamonds,
      );
      return result;
    } catch (error) {
      return {
        success: false,
        data: null,
        message: '充值失败: ' + error.message,
      };
    }
  }

  // 获取用户粉钻交易记录
  @UseGuards(AuthGuard('jwt'))
  @Get('transactions')
  async getUserDiamondTransactions(@Request() req) {
    try {
      const userId = parseInt(req.user.id);
      const result = await this.vipService.getUserDiamondTransactions(userId);
      return {
        success: true,
        data: result,
        message: '获取交易记录成功',
      };
    } catch (error) {
      return {
        success: false,
        data: null,
        message: '获取交易记录失败: ' + error.message,
      };
    }
  }

  // 创建 PayPal 充值订单
  @UseGuards(AuthGuard('jwt'))
  @Post('paypal/create')
  async createPaypalRechargeOrder(
    @Request() req,
    @Body() body: { amount: number; diamonds: number },
  ) {
    try {
      const userId = parseInt(req.user.id);
      const { amount, diamonds } = body;

      if (!amount || !diamonds) {
        throw new BadRequestException('缺少参数');
      }

      const result = await this.vipService.createPaypalRechargeOrder(
        userId,
        amount,
        diamonds,
      );
      return result;
    } catch (error) {
      return {
        success: false,
        data: null,
        message: '创建PayPal订单失败: ' + error.message,
      };
    }
  }

  // 执行 PayPal 充值
  @UseGuards(AuthGuard('jwt'))
  @Post('paypal/execute')
  async executePaypalRecharge(
    @Request() req,
    @Body() body: { orderId: string },
  ) {
    try {
      const userId = parseInt(req.user.id);
      const { orderId } = body;

      if (!orderId) {
        throw new BadRequestException('缺少订单ID');
      }

      const result = await this.vipService.executePaypalRecharge(
        userId,
        orderId,
      );
      return result;
    } catch (error) {
      return {
        success: false,
        data: null,
        message: '执行PayPal充值失败: ' + error.message,
      };
    }
  }
}
